package sv.mecca.modelo;

import java.sql.SQLException;
import java.util.ArrayList;
import sv.mecca.modelo.Conexion;
import sv.mecca.form.PacientesForm;

public class OperacionesPacientes extends Conexion{

    public void insertarPacientes(PacientesForm p) throws SQLException{
        try {
            st = getConexion().createStatement();
            st.execute("insert into pacientes(nombre, expediente, edad, sexo, procedencia) values(" +
                "'" + p.getNombre() + "', " +
                "'" + p.getExpediente() + "', " +
                "" + p.getEdad() + ", " +
                "'" + p.getSexo() + "', " +
                "'" + p.getProcedencia() + "')");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            }
        p.destroy();
        }
    public PacientesForm modificarPacientes(String ex) throws SQLException{
        PacientesForm p = new PacientesForm();
        try {
            st = getConexion().createStatement();
            rs = st.executeQuery("select * from pacientes where expediente like '" + ex + "'");
            rs.next();
            p.setExpediente(rs.getString(1));
            p.setNombre(rs.getString(2));
            p.setEdad(rs.getInt(3));
            p.setSexo(rs.getString(4));
            p.setProcedencia(rs.getString(5));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            rs.close();
            st.close();
            cn.close();
            return p;
            }
        }
    public void actualizarPacientes(PacientesForm p, String ex) throws SQLException{
        try {
            st = getConexion().createStatement();
            st.executeUpdate("update pacientes set " +
                "expediente = '" + p.getExpediente() + "', " +
                "nombre = '" + p.getNombre() + "', " +
                "edad = " + p.getEdad() + ", " +
                "sexo = '" + p.getSexo() + "', " +
                "procedencia = '" + p.getProcedencia() + "' " +
                "where expediente like '" + ex + "'");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            }
        p.destroy();
        }
    public void EliminarPacientes(String ex) throws SQLException{
        try {
            st = getConexion().createStatement();
            st.execute("delete from pacientes where expediente = '" + ex + "'");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            }
        }
    public ArrayList<PacientesForm> listado() throws SQLException{
        ArrayList<PacientesForm> paciente = null;
        try {
            st = getConexion().createStatement();
            rs = st.executeQuery("select * from pacientes");
            paciente = new ArrayList<PacientesForm>();
            while (rs.next()) {
                PacientesForm p = new PacientesForm(rs.getString(1), rs.getString(2), rs.getInt(3), rs.getString(4), rs.getString(5));
                paciente.add(p);
                }
            rs.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            return paciente;
            }
        }
    }